327A - Flipping Game - CodeForces Solution


brute force dp implementation *1200

Please click on ads to support us..

Python Code:

def solve(n,nums):
    ans = 0
    for i in range(n):
        for j in range(i,n):
            temp = 0
            for k in range(n):
                if k>=i and k<=j:
                    temp += 1-nums[k]
                else:
                    temp += nums[k]
            ans = max(ans,temp)
    return ans



if __name__ == '__main__':
    n = int(input())
    nums = list(map(int,input().split()))
    print(solve(n,nums))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define long long int ll
int main() {
	// your code goes here
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++)cin>>a[i];
	int ans=0;
	vector<int>v(n+1,0),pre(n+1,0);
	int cnt=0;
	for(int i=n-1;i>=0;i--){
	    if(a[i]==1)cnt++;
	    v[i]=cnt;
	}
	for(int i=0;i<n;i++){
	    pre[i+1]=a[i]+pre[i];
	    cnt=0;
	    for(int j=i;j<n;j++){
	        if(a[j]==0)cnt++;
	        ans=max(ans,v[j+1]+cnt+pre[i]);
	    }
	}
	cout<<ans;
	return 0;
}


Comments

Submit
0 Comments
More Questions

1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons